<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>出车统计</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        .layui-form-label {
            width: 100px;
        }
        .layui-input-inline {
            width: 200px;
        }
        .layui-table-cell {
            text-align: center;
        }
        .stat-table th {
            font-weight: bold;
            background-color: #f2f2f2;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">车牌号</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="license" placeholder="请输入车牌号">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">年份</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="year" placeholder="请输入年份" value="2024">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"
                                    lay-submit lay-filter="data-search-btn">
                                <i class="layui-icon">&#xe615;</i> 查询
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide stat-table" id="carStatisticsTable" lay-filter="carStatisticsFilter"></table>

        <script type="text/html" id="tableToolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="export">导出Excel</button>
            </div>
        </script>
    </div>
</div>

<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        // 初始化表格
        table.render({
            elem: '#carStatisticsTable',
            url: '/statistics/car/monthly',
            where: { year: $('#year').val() },
            loading: true,
            toolbar: '#tableToolbar',
            defaultToolbar: ['filter', 'print'],
            parseData: function(res) {
                return {
                    "code": 0,
                    "msg": "",
                    "count": res.length,
                    "data": res
                };
            },
            cols: [[
                {field: 'license', title: '车牌号', width: 120, align: 'center', sort: true},
                {field: 'month1', title: '1月', width: 90, align: 'center', sort: true},
                {field: 'month2', title: '2月', width: 90, align: 'center', sort: true},
                {field: 'month3', title: '3月', width: 90, align: 'center', sort: true},
                {field: 'month4', title: '4月', width: 90, align: 'center', sort: true},
                {field: 'month5', title: '5月', width: 90, align: 'center', sort: true},
                {field: 'month6', title: '6月', width: 90, align: 'center', sort: true},
                {field: 'month7', title: '7月', width: 90, align: 'center', sort: true},
                {field: 'month8', title: '8月', width: 90, align: 'center', sort: true},
                {field: 'month9', title: '9月', width: 90, align: 'center', sort: true},
                {field: 'month10', title: '10月', width: 90, align: 'center', sort: true},
                {field: 'month11', title: '11月', width: 90, align: 'center', sort: true},
                {field: 'month12', title: '12月', width: 90, align: 'center', sort: true},
                {field: 'total', title: '总计', width: 100, align: 'center', sort: true},
                {field: 'remark', title: '备注', width: 200, align: 'center'}
            ]],
            page: false, // 禁用分页
            skin: 'line',
            done: function(res, curr, count) {
                $(".layui-table-body").find("td").css("text-align", "center");
            }
        });

        // 搜索功能
        form.on('submit(data-search-btn)', function (data) {
            var license = $('#license').val();
            var year = $('#year').val();

            if (!year) {
                layer.msg('请输入年份', {icon: 2});
                return false;
            }
            if (isNaN(year)) {
                layer.msg('年份必须是数字', {icon: 2});
                return false;
            }

            table.reload('carStatisticsTable', {
                url: '/statistics/car/monthly',
                where: {
                    license: license,
                    year: parseInt(year)
                }
            });
            return false;
        });

        // 工具栏事件
        table.on('toolbar(carStatisticsFilter)', function (obj) {
            if (obj.event === 'export') {
                var license = $('#license').val();
                var year = $('#year').val();

                if (!year) {
                    layer.msg('请输入年份', {icon: 2});
                    return;
                }

                var url = '/statistics/car/export?' +
                    'license=' + encodeURIComponent(license || '') +
                    '&year=' + encodeURIComponent(year);

                window.location.href = url;
            }
        });
    });
</script>
</body>
</html>